<?php
class db{
    private $config;
    private $res;
    private $link_id;
    public function __construct($config) {
        $this->config = $config;
        $this->config['pconnect'] = 0;
        $this->config['charset'] = 'utf8';
        $this->connect();
    }
    
    public function connect() {
        if( $this->config['pconnect'] == 0 ) {
            $this->link_id = mysql_connect($this->config['host'], $this->config['user'], $this->config['pwd'], true);
            if(!$this->link_id){
                die("数据库连接失败");
            }
        }else{
            $this->link_id = mysql_pconnect($this->config['host'], $this->config['user'], $this->config['pwd']);
            if(!$this->link_id){
                die("数据库持久连接失败");
            }
        }
        if(!mysql_select_db($this->config['dbName'],$this->link_id)) {
            die('数据库选择失败');
        }
        mysql_query("SET NAMES ".$this->config['charset']);
    }
    
    public function Execute($sql) {
        $this->res = mysql_query($sql,$this->link_id);
        if(!$this->res) die('Query Error: ' . $sql);
        return $this->res;
    }
    
    public function FetchRow($res = null, $result_type = MYSQL_ASSOC){
        if(empty($res)){
            $res = $this->res;
        }
        $rows = mysql_fetch_row($res, $result_type);
        return $rows;
    }
}